<?php

class Application_Model_ProductType
{
    /**
     * @param bool $deleted
     * @return ProductTypePOJO[]
     */
    public static function getProductTypes($deleted = false)
    {
        $dbAdapter = JDbAdapter::factory();

        $productTypePOJOs = array();
        $assoc = $dbAdapter->fetchAll(
            $dbAdapter->select()
                ->from('PRODUCT_TYPES')
                ->where('Deleted = ?', $deleted)
        );
        foreach ($assoc as $row) {
            $productTypePOJO = new ProductTypePOJO($row);
            array_push($productTypePOJOs, $productTypePOJO);
        }

        return $productTypePOJOs;
    }

    /**
     * @param int $id
     * @param bool $deleted
     * @return ProductTypePOJO
     */
    public static function getProductType($id, $deleted = false)
    {
        $dbAdapter = JDbAdapter::factory();

        $row = $dbAdapter->fetchRow(
            $dbAdapter->select()
                ->from('PRODUCT_TYPES')
                ->where('ID = ?', $id)
                ->where('Deleted = ?', $deleted)
        );

        if ($row == null) {
            return null;
        }

        return new ProductTypePOJO($row);
    }

    /**
     * @param string $name
     * @param string $description
     * @return bool
     */
    public static function addProductType($name, $description)
    {
        $dbAdapter = JDbAdapter::factory();

        try {
            $dbAdapter->insert(
                'PRODUCT_TYPES',
                array('Name' => $name, 'Description' => $description)
            );

            return (int)$dbAdapter->lastInsertId('PRODUCT_TYPES');
        } catch (Exception $ex) {
            JLog::log($ex->getMessage());
            return 0;
        }
    }

    /**
     * @param int $id
     * @param string $name
     * @param string $description
     * @return bool
     */
    public static function updateProductType($id, $name, $description)
    {
        $dbAdapter = JDbAdapter::factory();

        try {
            $numberOfAffects = $dbAdapter->update(
                'PRODUCT_TYPES',
                array('Name' => $name, 'Description' => $description),
                "ID = $id"
            );

            if ($numberOfAffects == 0) {
                return false;
            }

            return true;
        } catch (Exception $ex) {
            JLog::log($ex->getMessage());
            return false;
        }
    }

    /**
     * @param int[] $ids
     * @return bool
     */
    public static function deleteProductTypes($ids)
    {
        $dbAdapter = JDbAdapter::factory();

        try {
            $str = '(';
            $length = count($ids);
            for ($i = 0; $i < $length - 1; $i++) {
                $str .= $ids[$i] . ', ';
            }
            $str .= $ids[$length - 1];
            $str .= ')';
            $numberOfAffects = $dbAdapter->update(
                'PRODUCT_TYPES', array('Deleted' => true), "ID IN $str"
            );
            if ($numberOfAffects == 0) {
                return false;
            }

            return true;
        } catch (Exception $ex) {
            JLog::log($ex->getMessage());
            return false;
        }
    }
}

class ProductTypePOJO extends POJO
{
    /** @var int */
    public $ID;

    /** @var string */
    public $Name;

    /** @var string */
    public $Description;

    /** @var bool */
    public $Deleted;

    /**
     * @return void
     */
    protected function convert()
    {
        $this->ID = (int)$this->ID;
        $this->Name = (string)$this->Name;
        $this->Description = (string)$this->Description;
        $this->Deleted = (bool)$this->Deleted;
    }
}